package com.fly.demo.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.annotation.JsonView;
import com.fly.common.PaginationSupport;
import com.fly.demo.entity.User;
import com.fly.demo.entity.User.Simple;
import com.fly.demo.service.UserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * RestUserController
 * 
 * @author 00fly
 * @version [版本号, 2018-11-06]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@Slf4j
@CrossOrigin
@Api(tags = "用户操作接口")
@RestController
@RequestMapping("/rest/user")
public class RestUserController
{
    @Autowired
    HttpServletRequest request;
    
    @Autowired
    UserService userService;
    
    @ApiOperation("查询用户")
    @JsonView(Simple.class)
    @GetMapping("/view/{id}")
    public User view(@PathVariable Long id)
    {
        User user = userService.queryById(id);
        return user;
    }
    
    @ApiOperation("分页查询用户")
    @JsonView(Simple.class)
    @GetMapping("/list/{pageNo}")
    public List<User> list(@PathVariable Integer pageNo, @RequestParam(defaultValue = "5") int pageSize)
    {
        pageSize = Math.max(pageSize, 2);
        List<User> list = userService.queryForPagination(new User(), pageNo, pageSize).getItems();
        return list;
    }
    
    /**
     * 列表展示<br>
     * unless 设置缓存不生效条件
     * 
     * @param model
     * @return
     * @see [类、类#方法、类#成员]
     */
    @ApiOperation("分页查询缓存的用户")
    @JsonView(Simple.class)
    @Cacheable(value = "user", key = "#pageNo", unless = "#result.size()<12")
    @GetMapping("/cache/list/{pageNo}")
    public List<User> listCached(@PathVariable int pageNo)
    {
        log.info("listCached run ......");
        PaginationSupport<User> page = userService.queryForPagination(null, pageNo, 10);
        return page.getItems();
    }
    
    @JsonView(Simple.class)
    @GetMapping("/all")
    public List<User> all()
    {
        List<User> list = userService.queryAll();
        return list;
    }
    
    /**
     * unless 设置缓存不生效条件
     * 
     * @return
     * @see [类、类#方法、类#成员]
     */
    @Cacheable(value = "user", key = "'cache'", unless = "#result['data'].size()<10")
    @JsonView(Simple.class)
    @GetMapping("/cache")
    public Object cache()
    {
        log.info("cache run ......");
        Map<String, Object> result = new HashMap<>();
        List<String> data = new ArrayList<>();
        for (int i = 0; i < 10; i++)
        {
            data.add(RandomStringUtils.randomAlphabetic(8));
        }
        result.put("code", "0");
        result.put("msg", "成功！");
        result.put("data", data);
        return result;
    }
}